package com.orangelife.mobile.hottrader.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.IXListViewListener;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.main.adapter.HomePageListViewAdapter;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

public class HotTraderActivity extends OrangeLifeBaseActivity {

	/** 商铺列表 */
	private PullToRefreshListView lvHotTrader;
	/** 标题 */
	private TextView tvTitle;
	private ImageView ivTitleScore;
	/** 返回按钮 */
	private LinearLayout ibBack;
	/** 首页热门商家列表的适配器 */
	private HomePageListViewAdapter<Map<String, Object>> adapter;
	/** 数据 */
	private List<Map<String, Object>> list;
	private List<Map<String, Object>> list2;
	private List<Map<String, Object>> list3;
	private Dialog dialog;
	private Handler mHandler;
	/** 一页中数据的数量 */
	private int pageSize = Constant.PAGE_SIZE_DEFAULLT;
	/** 第一页 */
	private int pageNo = Constant.PAGE_NO_DEFAULT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hot_trader);
		initeView();
		findView();
		dialog = DialogHelper.getInstance().createLoadingDialog(HotTraderActivity.this,
				Constant.LOADING);
		dialog.show();
		getHotTraderList(pageSize, pageNo);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/** 初始化组件 */
	private void initeView() {
		tvTitle = (TextView) findViewById(R.id.tv_title);
		ibBack = (LinearLayout) findViewById(R.id.ib_title_left);
		ivTitleScore = (ImageView)findViewById(R.id.iv_title_right);
		lvHotTrader = (PullToRefreshListView) findViewById(R.id.lv_hottrader);
	}

	/** 设置控件的值或状态 */

	private void findView() {
		tvTitle.setText("热门商家");
		pullListView();
		setAdapter();
		mHandler = new Handler();
	}

	/**
	 * 设置适配器
	 */
	private void setAdapter() {
		list = new ArrayList<Map<String, Object>>();
		list2 = new ArrayList<Map<String, Object>>();
		list3 = new ArrayList<Map<String, Object>>();
		adapter = new HomePageListViewAdapter<Map<String, Object>>(this, list);
		lvHotTrader.setPullLoadEnable(false);
		lvHotTrader.setAdapter(adapter);
	}

	/**
	 * 从后台获取界面的相应数据
	 */
	private void sendRequestForData() {

	}

	/**
	 * 下拉刷新列表
	 */
	private void pullListView() {
		lvHotTrader.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						list3 = list;
						list.clear();
						pageNo = 1;
						getHotTraderList(pageSize, pageNo);
						onLoad();
						adapter.setList(list);
						adapter.notifyDataSetChanged();
					}
				}, 2000);
			}

			@Override
			public void onLoadMore() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo += 1;
						getHotTraderList(pageSize, pageNo);
						onLoad();
						if (list != null) {
							adapter.setList(list);
							adapter.notifyDataSetChanged();
						}
					}
				}, 2000);
			}
		});
	}

	@SuppressWarnings("deprecation")
	private void onLoad() {
		lvHotTrader.stopRefresh();
		lvHotTrader.stopLoadMore();
		lvHotTrader.setRefreshTime(new Date().toLocaleString());
	}

	/**
	 * 刷新下拉列表
	 * 
	 * @param list2
	 */
	private void updateListView(List<Map<String, Object>> list2) {
		/** 判断list2是否为空 */
		if (list2 != null & list != null) {
			if (list2.size() == 0) {
				lvHotTrader.setPullLoadEnable(false);
				DialogHelper.closeDialog(dialog);
			} else {
				lvHotTrader.setPullLoadEnable(true);
				list.addAll(list2);
				adapter.setList(list);
				adapter.notifyDataSetChanged();
				DialogHelper.closeDialog(dialog);
			}
		} else {
			lvHotTrader.setPullLoadEnable(false);
		}
	}

	/** 接收并解析接收到的数据 */
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 5:
				list2 = JsonReponseHandler.getListFromJson(
						String.valueOf(msg.obj), adapter, dialog);
				if (list2 != null) {
					if (list2.size() == 0) {
						list2 = list3;
					}
					updateListView(list2);
				} else {
					updateListView(list3);
				}
				break;
			case 500:
				DialogHelper.closeDialog(dialog);
				ToastHelper.getInstance()._toast(
						msg.obj.toString());
				break;
			case 1011:
				isLogin(dialog);
				break;

			default:
				break;
			}
		}

	};

	/**
	 * 获取热门商家的数据
	 * 
	 */
	private void getHotTraderList(int pageSize, int pageNo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageIndex", pageNo);
		map.put("pageSize", pageSize);
		map.put("showLocation", "3");
		map.put("sourceType", "1");
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.URL_HOTTRADER);
		rootMap.put("wat", 5);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
		Log.e("pageNo", pageNo + "");
	}

}
